<?

if ($DATA['component']['id']) {
	
	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value

	$_crit = "id_data=$_id_data AND id_parent=$_id_component";

	$_param			= 'edit';				//	ID param name in the URL, used here & @ template
	$_values			= array('title', 'link');	// which fields are stored in language variations
	$_lang_indep	= array('blank');				// which fields are stored without language variations
	

	$s = array_extend( array('path' => 'images', 'tn' => FP_TN_DIR, 'w' => 800, 'h' => 600), $DATA['component']['settings']);
	$s['path'] = createdir(FP_UPLOAD_PATH.$s['path'].'/');
	
	$DATA['object'] = array('image' => false, 'data' => array());

	if ($d = datafetch_array('db_list', array('table' => tb('data'), 'where' => $_crit))) {
	
		foreach($d as $v)
			if ($v['assoc'])
				$DATA['object']['data'][$v['assoc']][$v['name']] = $v['value'];
			else
				$DATA['object'][$v['name']] = $v['value'];
		
		$DATA['object']['image'] = $DATA['object']['image'] ? checkImage($s['path'], $DATA['object']['image']) : false;
	}

	switch (action(array('save_object'))) {
	
		case 'save_object':

			$_record = array('id_data' => $_id_data, 'id_parent' => $_id_component);
			$temp = array();

			foreach($DATA['langs'] as $lang) {
				foreach($_values as $name) {
					$t = postdata($name);
					$temp[] = array_merge($_record, array('name' => $name, 'value' => $t[$lang['text']], 'assoc' => $lang['text']));
				}
			}

			foreach($_lang_indep as $name) {
				$t = trim(postdata($name));
				$temp[] = array_merge($_record, array('name' => $name, 'value' => $t));
			}

			if ($_FILES['image'] && ($fn = parse_fname($_FILES['image']['name'])) && !in_array(strtolower($fn['ext']), $DENIED_EXT) && ($im = takeImage('image', $s['w'], $s['h'], $s['path']))) {

				$temp[] = array_merge($_record, array('name' => 'image', 'value' => $im['filename']));
				
				if ($DATA['object']['image'] && ($DATA['object']['image']['fn'] != $im['fn'])) 
					unlink("$_SERVER[DOCUMENT_ROOT]{$DATA['object'][image][fn]}");
					
			} else 
				if ($DATA['object']['image']) {
					$fn = parse_fname($DATA['object']['image']['fn']);
					$temp[] = array_merge($_record, array('name' => 'image', 'value' => "$fn[name].$fn[ext]"));
				}
				
			dataset('db_delete', tb('data'), $_crit);
			dataset('db_insert', tb('data'), $temp);

			return true;
	}


	return false;

} else exit;

?>